// pages/my/addressManagement/addressManagement.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */ 
  data: {
    addressList: [],
    addressInformation: null,
    checked: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    this.getList();
  },
  //地址列表
  getList:function(){
    
    let self = this;
    app.util.ajax({
      url: 'api/Address/addressList',
    }, function (res) { 
      let data = res.data.data;

      for(var i = 0;i<data.length;i++){
        let is_default = data[i].is_default;
        let checkBool = is_default == '1' ? 'true' : '';
        data[i].is_check = checkBool;
      }
      self.setData({
        addressInformation: data,
      })
     
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
  //页面跳转
  jumpAddress:function(e){
    let id = e.currentTarget.dataset.id;
      app.util.pageJump('/pages/my/addTheAddress/addTheAddress?id=' + id);
    
  },

  //删除地址
  deleteInformation: function(e) {
    let self = this,
      index = e.currentTarget.dataset.index,
    itemId = self.data.addressInformation[index].address_id;
    app.util.ajax({
      url: 'api/Address/addressDelete',
      data: {
        address_id: itemId
      }
    },function(res){
        self.getList();  
    })
  },

  //设置默认地址
  defaultAddress:function(e){
    let self = this,
      index = e.currentTarget.dataset.index,
      itemId = self.data.addressInformation[index].address_id,
      isDefault = self.data.addressInformation[index].is_default;
    isDefault = isDefault == '0' ? '1' : '0';
    app.util.ajax({
      url: 'api/Address/addressSetDefault',
      data:{
        address_id: itemId,
        is_default:isDefault
      }
    },function(res){
      self.getList();
      
    })
  }
})